#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from google.appengine.ext import db

import webapp2

global VERSION
VERSION = "159"

global REDIRECTURL
REDIRECTURL = ("http://d5e40jlk470gqc84t9dxj57fdh.hop.clickbank.net/",
               "http://ed93c7uq182rv86pv2pht4wbt9.hop.clickbank.net/", #Translator
               "http://fiverr.com/magisterquis/advertise-for-two-hours-your-business-website-or-whatever-else-you-would-like-on-omegle-to-drive-traffic-to-you",
               "http://a24009tgx5-ttgd2r-0oucwm5y.hop.clickbank.net/", #girls
               "http://112dadjf735nqj7jx9m6jwwt4u.hop.clickbank.net/", #Truth about abs
               "http://78343dyjx71mjj6no7ojn40tam.hop.clickbank.net/", #Bigger penis
               "http://115707yrz-spmg5ajov2iaekev.hop.clickbank.net/", #Diablo 3
               )

class Use(db.Model):
    dtg = db.DateTimeProperty(auto_now_add=True)
    ip = db.StringProperty(required=True)
    action = db.StringProperty(required=True, default="")
    value = db.StringProperty(default="")
    
class MainHandler(webapp2.RequestHandler):
    def get(self):
        value = self.request.get("value")
        if value is None: value = ""
        Use(ip=self.request.remote_addr,
            action=self.request.get("action"),
            value=value
            ).put()
        self.response.out.write("%s\n%s\n%s\n"%(self.request.remote_addr,
                                                self.request.get("action"),
                                                value,))
        
class VersionHandler(webapp2.RequestHandler):
    def get(self):
        version = self.request.get("version")
        if version == "": version = "unk"
        if version == VERSION:
            version = version + " :)"
        else:
            version = version + " :( " + VERSION
        Use(ip=self.request.remote_addr,
            action="versioncheck",
            value = version).put()
        self.response.out.write(str(VERSION))
        
        
class Redirector(webapp2.RequestHandler):
    def get(self):
        try:
            url = str(REDIRECTURL[int(self.request.get("index"))])
        except:
            import random
            random.seed()
            url = REDIRECTURL[random.randint(0,len(REDIRECTURL)-1)]
        self.redirect(url)
        return
    def post(self):
        self.get()

app = webapp2.WSGIApplication([('/log', MainHandler),
                               ('/version', VersionHandler),
                               ('/redirector', Redirector),],
                              debug=True)
